import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.eclipse.core.internal.resources.FileState;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.internal.core.jdom.CompilationUnit;
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;

public class ArkinTemplate {

	public StringBuilder imports = new StringBuilder();
	public StringBuilder constructor = new StringBuilder();
	public StringBuilder attributes = new StringBuilder();
	public StringBuilder methods = new StringBuilder();
	public StringBuilder methods_states = new StringBuilder();
	public StringBuilder run = new StringBuilder();
	public String className;
	public String extendz;

	public void write() throws IOException {

		
		File file = new File(className+".java"); 
		FileWriter fw = new FileWriter(file);
		StringBuilder sb = new StringBuilder();
		
		fw.append("/**\n");
		fw.append("* Cette classe a ete generee par Arkin Generator\n");
		fw.append("* @author Nahed Chahine Laventure Aurelien\n");
		fw.append("*/\n");
		fw.append("\n//IMPORT\n");
		fw.append(imports.toString());

		fw.append("\n//CLASS\n");
		fw.append("public class " + className + " extends "
				+ extendz.toString() + " {\n\n");

		fw.append("\n//ATTRIBUTES\n");
		fw.append(attributes.toString());

		fw.append("\n//CONSTRUCTOR\n");
		fw.append("public " + className + "() {\n");
		fw.append(constructor.toString());
		fw.append("}\n\n");

		fw.append("\n//METHODS\n");
		fw.append(methods.toString());

		fw.append(methods_states.toString());

		fw.append("\n//RUN\n");
		fw.append("public void run() {\n\n");
		fw.append("//Main control loop\n");
		fw.append("while (step(" + Visitor.TIME_STEP + ") != -1) {\n");
		fw.append(run.toString());
		fw.append("}\n");
		fw.append("}\n");
		fw.append("}\n");
		fw.flush();
		fw.close();

		/*String str = sb.toString();

		CodeFormatter formatter = new DefaultCodeFormatter();

		TextEdit indent_edit = formatter.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, str,
				0, str.length(), 0, System.getProperty("line.separator"));
		IDocument dc = new Document(str);
		try {
			System.err.println(dc.get());
			System.err.println(indent_edit);
			indent_edit.apply(dc);
			System.err.println(dc.get());
		} catch (MalformedTreeException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}*/

		/*
		 * fw.write(dc.get()); fw.flush(); fw.close();
		 */

	}

	/*
	 * b.run.append("public void run(){" + "while(step(timeStep)!=-1){" + "" +
	 * "" + "" + "}}"
	 */
}
